package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.After;
import org.junit.Test;

/**
 * Test the MyIsernDatabase.
 * 
 * @author Randy Cox
 * 
 */
public class TestDatabaseResearchers {

  /**
   * Test the dropping and creating of researcher tables.
   */
  @Test
  public void testCreateDropTable() {
    DatabaseResearchers db = new DatabaseResearchers();
    db.createTables();
    assertTrue("Check that table is there.", db.hasTableResearcher());
    db.createTables();
    assertTrue("Check that table is still there.", db.hasTableResearcher());
    db.dropTables();
    assertFalse("Check that table is gone.", db.hasTableResearcher());
    db.dropTables();
    assertFalse("Check that no error happens.", db.hasTableResearcher());
  }

  /**
   * Test the insert, update, query etc for the researcher table.
   */
  @Test
  public void testGetSave() {
    DatabaseResearchers db = new DatabaseResearchers();

    // Set up test strings.
    String id = ("FIRST_LAST");
    String name = "First Last ";
    String email = "Email";
    String org = "Org";
    String pict = "Pict";
    String bio = "Bio";

    // Set up record vars.
    MyResearcher researcher = new MyResearcher();
    researcher.setName(name);
    researcher.setEmail(email);
    researcher.setOrganization(org);
    researcher.setPictureLink(pict);
    researcher.setBio(bio);

    // Save researcher and check count.
    db.save(researcher);
    assertTrue("Check if researcher found after save.", db.hasResearcher(id));

    // Retrieve researcher back and check values.
    MyResearcher researcher2 = db.get(id);
    assertEquals("Check ID.", id, researcher2.getId());
    assertEquals("Check name.", name.trim(), researcher2.getName());
    assertEquals("Check email.", email, researcher2.getEmail());
    assertEquals("Check organization.", org, researcher2.getOrganization());
    assertEquals("Check pictureLink.", pict, researcher2.getPictureLink());
    assertEquals("Check bio.", bio, researcher2.getBio());

    // Test for non-existant researcher.
    assertFalse("Check non-existant researcher.", db.hasResearcher("BAD_ID"));
    assertNull("Check getting non-existant researcher.", db.get("BAD_ID2"));

    // Test update.
    db.save(researcher);
    assertTrue("Check if researcher found after save.", db.hasResearcher(id));

    // Retrieve researcher back and check values.
    MyResearcher researcher3 = db.get(id);
    assertEquals("Check ID.", id, researcher3.getId());
    assertEquals("Check name.", name.trim(), researcher3.getName());
    assertEquals("Check email.", email, researcher3.getEmail());
    assertEquals("Check organization.", org, researcher3.getOrganization());
    assertEquals("Check pictureLink.", pict, researcher3.getPictureLink());
    assertEquals("Check bio.", bio, researcher3.getBio());
  }

  /**
   * Test the insert, update, query etc for the researcher table.
   */
  @Test
  public void testClearDeleteListCount() {
    DatabaseResearchers db = new DatabaseResearchers();
    db.clear();

    // Set up test strings.
    // Set up test strings.
    String name = "First Last ";
    String email = "Email";
    String org = "Org";
    String pict = "Pict";
    String bio = "Bio";

    // Set up record vars.
    MyResearcher researcher = new MyResearcher();
    researcher.setName(name);
    researcher.setEmail(email);
    researcher.setOrganization(org);
    researcher.setPictureLink(pict);
    researcher.setBio(bio);

    // Save organization and check count.
    Integer expected = 0;
    assertEquals("Check that db is empty.", expected, db.count());
    researcher.setName("name1");
    db.save(researcher);
    expected++;
    assertEquals("Check that db has 1 record.", expected, db.count());

    // Save 2nd record
    researcher.setName("name2");
    db.save(researcher);
    expected++;
    assertEquals("Check that db has 2 records.", expected, db.count());

    // Save 3rd record
    researcher.setName("name3");
    db.save(researcher);
    expected++;
    assertEquals("Check that db has 3 records.", expected, db.count());

    // Check list function
    List<MyResearcher> results = db.list();
    assertEquals("Check record 1", "NAME1", results.get(0).getId());
    assertEquals("Check record 2", "NAME2", results.get(1).getId());
    assertEquals("Check record 3", "NAME3", results.get(2).getId());

    // Delete 2nd record
    researcher.setName("name2");
    db.delete("NAME2");
    expected--;
    assertEquals("Check that db has 2 records.", expected, db.count());
    assertFalse("Check that record 2 is gone", db.hasResearcher("NAME2"));

    db.clear();
    expected = 0;
    assertEquals("Check that db is empty.", expected, db.count());
  }

  /**
   * Drop test tables.
   */
  @After
  public void cleanUpTables() {
    DatabaseResearchers db = new DatabaseResearchers();
    db.dropTables();
  }

}
